package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.domain.JobDescriptor;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class DeleteJobAction extends AbstractFormAction {

    private JobDescriptor job;

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadAndBind() {
        String id = getContext().getRequest().getParameter("job.id");
        int jobId = Integer.parseInt(id);
        job = getJobService().getJob(jobId);
    }

    public JobDescriptor getJob() {
        return job;
    }

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution(DELETE_JOB_JSP);
    }

    @HandlesEvent("delete")
    public Resolution onSave() {
        getJobService().delete(job);
        return new ForwardResolution(HOME_ACTION);
    }
}
